var signManage = {
    init: function() {
        var that = this;
        that.getAddress(10, '', $("select[data-name=Mprovince]"));
        that.getAddress(20, '', $("select[data-name=Mcity]"));
        that.getAddress(30, '', $("select[data-name=Mtown]"));
        $.ajaxSend('api/Contract/GetList?token=' + $.cookie('token'), {
            "page": 1,
            "pagesize": 20,
            "companyname": '',
            "cityid": ''
        }, function(redata) {
            if (redata.recode == '0000' && redata.mod.DataObj.length>0) {
                var compiled = _.template($("#signManageTpl").html());
                var odata = redata.mod.DataObj;
                $("#signManageCon").html(compiled(redata.mod));
                $('#closemangModel').on('show.bs.modal', function(event) {
                    var button = $(event.relatedTarget),
                        company_id = button.data('company_id'),
                        cancel_id =button.data('cancel_id'),
                        wuliu_id = button.data('wuliu_id');
                       
                    $(this).attr({'company_id':company_id,'wuliu_id':wuliu_id,'cancel_id':cancel_id});
                });


                $("#closeManageForm").validate({
                   errorClass: 'error',
                   rules: {
                      closeReson:'required'
                   },
                    messages: {
                      closeReson:"填写正确的解约原因"
                      
                    },
                    submitHandler: function(form) {
                         var company_id = $('#closemangModel').attr('company_id'),
                             wuliu_id = $('#closemangModel').attr('wuliu_id'),
                              id=$('#closemangModel').attr('cancel_id'),
                              cancel_reason = $("#closeReson").val(),

                              reason_info = $.trim($("#closemangText").val());

                          $.ajax({   //回单确认提交
                              url: COMMONURL + "api/Contract/Cancel?token=" + $.cookie('token'),
                              type: "post",
                              data: {
                                  company_id: company_id,
                                            wuliu_id:wuliu_id,
                                            cancel_reason:cancel_reason,
                                            reason_info: reason_info,
                                  	      id:id
                              }
                          }).done(function(redata) {
                                        if(redata.recode == '0000'){
                                          alert('操作成功');
                                          $('#closemangModel').modal('hide');
                                          window.location.reload();
                                        }
                          });
                    }
                })





                $("#signManagePage").show().jqPaginator({
                    //totalPages: Math.ceil(redata.mod.TotalNum/20),
                    totalPages: redata.mod.TotalPageCount,
                    visiblePages: 5,
                    currentPage: 1,
                    first: '<li class="first"><a href="javascript:void(0);">首页<\/a><\/li>',
                    prev: '<li class="prev"><a href="javascript:void(0);"><i class="arrow arrow2"><\/i>上一页<\/a><\/li>',
                    next: '<li class="next"><a href="javascript:void(0);">下一页<i class="arrow arrow3"><\/i><\/a><\/li>',
                    last: '<li class="last"><a href="javascript:void(0);">末页<\/a><\/li>',
                    page: '<li class="page"><a href="javascript:void(0);">{{page}}<\/a><\/li>',
                    onPageChange: function(num,type) {
                        $("#signManagePage").attr('curpage', num);
                        $("#smpage").val(num);
                        if(type =='change'){
                            that.doPage();
                        }
                    }
                });
            }else{
                var compiled = _.template($("#signManageTpl").html());
                var odata = redata.mod.DataObj;
                $("#signManageCon").html(compiled(redata.mod));

            }
        });

        $("#signManagequery").click(function() {
            that.doSearch();
        });
    },
    getAddress: function(type, sub, obj) {
        $.ajax({
            url: COMMONURL + 'api/CityBase/GetCityBaseList?token=' + $.cookie('token'),
            type: 'post',
            dataType: 'json',
            data: {
                type: type,
                sub_city_id: sub
            }

        }).done(function(data) {
            var arry = [];
            if (!!data.mod) {
                if (!data.mod.length) {
                    arry.push('<option value="' + '" data-id="' + '">' + '请选择' + '</option>');
                } else {
                    $.each(data.mod, function(i, v) {
                        arry.push('<option value="' + v.city_name + '" data-id="' + v.city_id + '">' + v.city_name + '</option>');
                    });
                }
                obj.html(arry.join(''));
            }

        })
    },
    doPage:function(){
        var page = $.trim($("#smpage").val());
        $.ajax({
            url: COMMONURL + "api/Contract/GetList?token=" + $.cookie('token'),
            type: "post",
            data: {
                page: page,
                pagesize: 20,
                companyname: '',
                cityid: ''
            },
            dataType: "json"
        }).done(function(redata) {
                if(redata.recode=='1003'){
                window.location.href = '/nlogin.html';
                return;}
            if (redata.recode == '0000' && redata.mod.DataObj.length>0) {
                var compiled = _.template($("#signManageTpl").html());
                var odata = redata.mod.DataObj;
                $("#signManageCon").html(compiled(redata.mod));
            }else{
               var compiled = _.template($("#signManageTpl").html());
               
                $("#signManageCon").html(compiled(redata.mod));
                $("#signManagePage").hide();
            }
        });
    },
    doSearch: function() {
        var page = $.trim($("#smpage").val());
        $.ajax({
            url: COMMONURL + "api/Contract/GetList?token=" + $.cookie('token'),
            type: "post",
            data: {
                page: page,
                pagesize: 20,
                companyname: '',
                cityid: ''
            },
            dataType: "json"
        }).done(function(redata) {
        		if(redata.recode=='1003'){
                window.location.href = '/nlogin.html';
                return;}
            if (redata.recode == '0000') {
                var compiled = _.template($("#signManageTpl").html());
                var odata = redata.mod.DataObj;
                $("#signManageCon").html(compiled(redata.mod));
                $("#signManagePage").show().jqPaginator({
                    //totalPages: Math.ceil(redata.mod.TotalNum/20),
                    totalPages: redata.mod.TotalPageCount,
                    visiblePages: 5,
                    currentPage: 1,
                    first: '<li class="first"><a href="javascript:void(0);">首页<\/a><\/li>',
                    prev: '<li class="prev"><a href="javascript:void(0);"><i class="arrow arrow2"><\/i>上一页<\/a><\/li>',
                    next: '<li class="next"><a href="javascript:void(0);">下一页<i class="arrow arrow3"><\/i><\/a><\/li>',
                    last: '<li class="last"><a href="javascript:void(0);">末页<\/a><\/li>',
                    page: '<li class="page"><a href="javascript:void(0);">{{page}}<\/a><\/li>',
                    onPageChange: function(num, type) {
                        $("#signManagePage").attr('curpage', num);
                        $("#smpage").val(num);
                        if(type =='change'){
                            that.doPage();
                        }
                    }
                });
            }else{
               var compiled = _.template($("#signManageTpl").html());
               
                $("#signManageCon").html(compiled(redata.mod));
                $("#signManagePage").hide();
            }
        });

    }
}


signManage.init();
